﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;

using LetLord.Controllers;
using FluentSecurity;

namespace LetLord
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            // Remove Web Forms View Engine to improve performance
            var viewEngines = System.Web.Mvc.ViewEngines.Engines;
            var webFormsEngine = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
            if (webFormsEngine != null)
            {
                viewEngines.Remove(webFormsEngine);
            }

            // //FluentSecurity configuration
            //SecurityConfigurator.Configure(configuration =>
            //    {
            //         // By default FluentSecurity throws an exception if a missing configuration is found for a controller
            //         // action. This ignores that for development. Should be left out for production.
            //        //configuration.IgnoreMissingConfiguration();

            //         // Gets authentication status of current user
            //        configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);

            //         // Get roles of current user
            //        configuration.GetRolesFrom(() => Roles.GetRolesForUser());

            //         //***Set up policies to enforce on controllers and actions***\\

            //         // Allow anonymous users on home page
            //        configuration.For<HomeController>().Ignore();
            //        configuration.For<SearchController>().Ignore();
            //        configuration.For<ResidentialPropertiesController>().Ignore();

            //         // Only tenants on tenant page
            //        configuration.For<TenantsController>().RequireRole("Tenant");
            //        // Only landlords on landlord page
            //        configuration.For<LandlordsController>().RequireRole("Landlord");
            //        // Only tenants and landlords use account controller
            //        configuration.For<AccountController>().RequireRole("Landlord", "Tenant");

            //    });

            //GlobalFilters.Filters.Add(new HandleSecurityAttribute(), 0);
        }
    }
}